package com.zzty.servlet;

import com.zzty.common.Log;
import com.zzty.dao.CompetionDao;
import com.zzty.daoimpl.CompetionDaoImpl;
import com.zzty.entity.Question;
import com.zzty.entity.SelfExam;
import com.zzty.service.ComService;
import com.zzty.serviceimpl.ComServiceImpl;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import static java.lang.Integer.parseInt;

@WebServlet(name="SelfServlet",urlPatterns="/SelfServlet")
public class SelfServlet extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(Log.class);
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if("setQuestion".equals(method)){
            try {
                setQuestion(request,response);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if("getTable".equals(method)){
            getTable(request,response);
        }
        if("checkDate".equals(method)){
            checkDate(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }
    protected void setQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String tquestionId=request.getParameter("tquestionId");
        String date=request.getParameter("tdate");
        Integer classId=parseInt(request.getParameter("classId"));
        CompetionDao cd = new CompetionDaoImpl();
        boolean result=cd.insertQuestionId(tquestionId,classId,date);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        PrintWriter out = response.getWriter();
        logger.info("json:"+json);
        out.write(json);
        out.flush();
        out.close();
    }
    protected void getTable(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ComService cs=new ComServiceImpl();
        List<SelfExam> selfExams=null;
        String json=null;
        ObjectMapper mapper = new ObjectMapper();
        if(cs.getSelfExamToTable()!=null){
            selfExams=cs.getSelfExamToTable();
            json = mapper.writeValueAsString(selfExams);
        }
        PrintWriter out = response.getWriter();
        logger.info("json:"+json);
        out.write(json);
        out.flush();
        out.close();
    }
    protected void checkDate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String date=request.getParameter("date");
        ComService cs=new ComServiceImpl();
        boolean result=cs.checkDate(date);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        PrintWriter out = response.getWriter();
        logger.info("json:"+json);
        out.write(json);
        out.flush();
        out.close();
    }


}
